# -*- coding: utf-8 -*-
# @File     : throttling.py
# @Author   : bingjia
# @Time     : 2020/7/24 8:56
# @Desc     : drf 节流，提供节流基类以及其它节流策略

import time
from ipware.ip import get_ip
from django.conf import settings
from netaddr import all_matching_cidrs
from netaddr.core import AddrFormatError
from django.core.cache import cache as default_cache
from django.core.exceptions import ImproperlyConfigured

from .settings import api_settings


class BaseThrottle(object):
    """
    request节流的基类
    """
    def allow_request(self, request, view):
        """
        如果request请求被允许则返回'True',否则返回'False'
        """
        raise NotImplementedError(".allow_request() must be overridden")

    def finalize(self, request, response, view):
        """
        Optionally, update the Trottling information based on de response.
        """
        return None

    def wait(self):
        """
        返回需等待的秒数
        """
        return None


class SimpleRateThrottle(BaseThrottle):
    """
    A simple cache implementation, that only requires `.get_cache_key()`
    to be overridden.

    The rate (requests / seconds) is set by a `throttle` attribute on the View
    class.  The attribute is a string of the form "number_of_requests/period".

    Period should be one of: ("s", "sec", "m", "min", "h", "hour", "d", "day")

    Previous request information used for throttling is stored in the cache.
    """
    scope = None
    timer = time.time()
    cache = default_cache
    cache_format = "throtte_%(scope)s_%(ident)s"
    THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES

    def __init__(self):
        if not getattr(self, "rate", None):
            self.rate = self.get_rate()
        self.num_requests, self.duration = self.parse_rate(self.rate)

    def get_cache_key(self, request, view):
        """
        Should return a unique cache-key which can be used for throttling.
        Must be overridden.

        May return `None` if the request should not be throttled.
        """
        raise NotImplementedError(".get_cache_key() must be overridden")

    def has_to_finalize(self, request, response, view):
        """
        Determine if the finalize method must be executed.
        """
        return self.rate is not None

    def get_rate(self):
        """
        Determine the string representation of the allowed request rate.
        """
        if not getattr(self, "scope", None):
            msg = ("You must set either `.scope` or `.rate` for \"%s\" throttle" %
                   self.__class__.__name__)
            raise ImproperlyConfigured(msg)

        try:
            return self.THROTTLE_RATES[self.scope]
        except KeyError:
            msg = "No default throttle rate set for \"%s\" scope" % self.scope
            raise ImproperlyConfigured(msg)

    def parse_rate(self, rate):
        """
        Given the request rate string, return a two tuple of:
        <allowed number of requests>, <period of time in seconds>
        """
        if rate is None:
            return (None, None)
        num, period = rate.split("/")
        num_requests = int(num)
        duration = {"s": 1, "m": 60, "h": 3600, "d": 86400}[period[0]]
        return (num_requests, duration)

    def allow_request(self, request, view):
        """
        Implement the check to see if the request should be throttled.

        On success calls `throttle_success`.
        On failure calls `throttle_failure`.
        """
        if self.rate is None:
            return True

        self.key = self.get_cache_key(request, view)
        if self.key is None:
            return True

        self.history = self.cache.get(self.key, [])
        self.now = self.timer()

        # Drop any requests from the history which have now passed the
        # throttle duration
        while self.history and self.history[-1] <= self.now - self.duration:
            self.history.pop()

        if self.exceeded_throttling_restriction(request, view):
            return self.throttle_failure()
        return self.throttle_success(request, view)

    def exceeded_throttling_restriction(self, request, view):
        return len(self.history) >= self.num_requests

    def throttle_success(self, request, view):
        """
        Inserts the current request's timestamp along with the key
        into the cache.
        """
        self.history.insert(0, self.now)
        self.cache.set(self.key, self.history, self.duration)
        return True

    def throttle_failure(self):
        """
        Called when a request to the API has failed due to throttling.
        """
        return False

    def wait(self):
        """
        Returns the recommended next request time in seconds.
        """
        if self.history:
            remaining_duration = self.duration - (self.now - self.history[-1])
        else:
            remaining_duration = self.duration

        available_requests = self.num_requests - len(self.history) + 1
        if available_requests <= 0:
            return None

        return remaining_duration / float(available_requests)


class AnonRateThrottle(SimpleRateThrottle):
    """
    Limits the rate of API calls that may be made by a anonymous users.

    The IP address of the request will be used as the unique cache key.
    """
    scope = "anon"

    def get_cache_key(self, request, view):
        if request.user.is_authenticated:
            return None  # Only throttle unauthenticated requests.

        ident = request.META.get("HTTP_X_FORWARDED_FOR")
        if ident is None:
            ident = request.META.get("REMOTE_ADDR")

        return self.cache_format % {
            "scope": self.scope,
            "ident": ident
        }


class UserRateThrottle(SimpleRateThrottle):
    """
    Limits the rate of API calls that may be made by a given user.

    The user id will be used as a unique cache key if the user is
    authenticated.  For anonymous requests, the IP address of the request will
    be used.
    """
    scope = "user"

    def get_cache_key(self, request, view):
        if request.user.is_authenticated:
            ident = request.user.id
        else:
            ident = request.META.get("REMOTE_ADDR", None)

        return self.cache_format % {
            "scope": self.scope,
            "ident": ident
        }


class ScopedRateThrottle(SimpleRateThrottle):
    """
    Limits the rate of API calls by different amounts for various parts of
    the API.  Any view that has the `throttle_scope` property set will be
    throttled.  The unique cache key will be generated by concatenating the
    user id of the request, and the scope of the view being accessed.
    """
    scope_attr = "throttle_scope"

    def __init__(self):
        # Override the usual SimpleRateThrottle, because we can't determine
        # the rate until called by the view.
        pass

    def allow_request(self, request, view):
        # We can only determine the scope once we"re called by the view.
        self.scope = getattr(view, self.scope_attr, None)

        # If a view does not have a `throttle_scope` always allow the request
        if not self.scope:
            return True

        # Determine the allowed request rate as we normally would during
        # the `__init__` call.
        self.rate = self.get_rate()
        self.num_requests, self.duration = self.parse_rate(self.rate)

        # We can now proceed as normal.
        return super(ScopedRateThrottle, self).allow_request(request, view)

    def get_cache_key(self, request, view):
        """
        If `view.throttle_scope` is not set, don't apply this throttle.

        Otherwise generate the unique cache key by concatenating the user id
        with the ".throttle_scope` property of the view.
        """
        if request.user.is_authenticated:
            ident = request.user.id
        else:
            ident = request.META.get("REMOTE_ADDR", None)

        return self.cache_format % {
            "scope": self.scope,
            "ident": ident
        }


class CommonThrottle(SimpleRateThrottle):
    cache_format = "throtte_%(scope)s_%(rate)s_%(ident)s"

    def __init__(self):
        pass

    def has_to_finalize(self, request, response, view):
        return False

    def is_whitelisted(self, ident):
        """
        是否是白名单
        :param ident: id
        :return:
        """
        for whitelisted in settings.REST_FRAMEWORK['DEFAULT_THROTTLE_WHITELIST']:
            if isinstance(whitelisted, int) and whitelisted == ident:
                return True
            elif isinstance(whitelisted, str):
                try:
                    if all_matching_cidrs(ident, [whitelisted]) != []:
                        return True
                except(AddrFormatError, ValueError):
                    pass
        return False

    def allow_request(self, request, view):
        scope = self.get_scope(request)
        ident = self.get_ident(request)
        rates = self.get_rates(scope)

        if self.is_whitelisted(ident):
            return True

        if rates is None or rates == []:
            return True

        now = self.timer()

        waits = []
        history_writes = []

        for rate in rates:
            rate_name = rate[0]
            rate_num_requests = rate[1]
            rate_duration = rate[2]

            key = self.get_cache_key(ident, scope, rate_name)
            history = self.cache.get(key, [])

            while history and history[-1] <= now - rate_duration:
                history.pop()

            if len(history) >= rate_num_requests:
                waits.append(self.wait_time(history, rate, now))

            history_writes.append({
                "key": key,
                "history": history,
                "rate_duration": rate_duration,
            })

        if waits:
            self._wait = max(waits)
            return False

        for history_write in history_writes:
            history_write['history'].insert(0, now)
            self.cache.set(
                history_write['key'],
                history_write['history'],
                history_write['rate_duration']
            )
        return True

    def get_rates(self, scope):
        try:
            rates = self.THROTTLE_RATES[scope]
        except KeyError:
            msg = "No default throttle rate set for \"%s\" scope" % scope
            raise ImproperlyConfigured(msg)

        if rates is None:
            return []
        elif isinstance(rates, str):
            return [self.parse_rate(rates)]
        elif isinstance(rates, list):
            return list(map(self.parse_rate, rates))
        else:
            msg = "No valid throttle rate set for \"%s\" scope" % scope
            raise ImproperlyConfigured(msg)

    def parse_rate(self, rate):
        """
        Given the request rate string, return a two tuple of:
        <allowed number of requests>, <period of time in seconds>
        """
        if rate is None:
            return None
        num, period = rate.split("/")
        num_requests = int(num)
        duration = {"s": 1, "m": 60, "h": 3600, "d": 86400}[period[0]]
        return (rate, num_requests, duration)

    def get_scope(self, request):
        scope_prefix = "user" if request.user.is_authenticated else "anon"
        scope_sufix = "write" if request.method in ["POST", "PUT", "PATCH", "DELETE"] else "read"
        scope = "{}-{}".format(scope_prefix, scope_sufix)
        return scope

    def get_ident(self, request):
        if request.user.is_authenticated:
            return request.user.id
        ident = get_ip(request)
        return ident

    def get_cache_key(self, ident, scope, rate):
        return self.cache_format % { "scope": scope, "ident": ident, "rate": rate }

    def wait_time(self, history, rate, now):
        rate_num_requests = rate[1]
        rate_duration = rate[2]

        if history:
            remaining_duration = rate_duration - (now - history[-1])
        else:
            remaining_duration = rate_duration

        available_requests = rate_num_requests - len(history) + 1
        if available_requests <= 0:
            return remaining_duration

        return remaining_duration / float(available_requests)

    def wait(self):
        return self._wait

